<?php 
class TasteListAction extends Action {

	public function _initialize(){
		$action = array(
			'permission'=>array(),
			'allow'=>array('getcustomerlist')
		);
		B('Authenticate', $action);
	}

	/*无法验证中文
	public function checkName(){
		$customer = M('customer');
		if ($customer->where('name = "' . $_GET['name'] . '"' )->find()){
			$this->ajaxReturn(1, "用户名不可以使用啊！",  1);
		}else{
			$this->ajaxReturn(0, "用户名可以使用！", 0);
		}
	}
	*/

    public function index()
    {
        $listRows = 30;
        $p = isset($_GET['p']) ? intval($_GET['p']) : 1 ;
        $taste = D("Taste");

        $fields = $taste->getDbFields();
        $this->assign("fields", $fields);

        //$list = $taste->order("id desc")->page($p.','.$listRows)->select();
        $list = M()->table("5k_taste t")->join("5k_customer c on c.customer_id=t.customer_id")->field("c.name, t.*")
            ->where("t.status=".TasteModel::TOPASS)
            ->order("t.id desc")->page($p.",".$listRows)->select();

        $this->assign("list", $list);

        $count = $taste->count();
        import("@.ORG.Page");
        $Page = new Page($count, $listRows);

        $this->assign('page', $Page->show());
        $this->alert = parseAlert();
        $this->display(); // 输出模板
    }

    public function finish(){

        $id = $this->_request("id");

        //反馈给总经理助理销售经理及提交试吃申请的销售人员

        //提交试吃申请的销售人员id
        $user_id=M('taste')->where(array('id'=>$id))->getField('applier_id');
        //提交试吃申请的销售人员role_id
        $role_id=M('role')->where(array('user_id'=>$user_id))->getField('role_id');

        /**
         * 获取销售人员的销售经理的role_id
         */
        //获取用户的position_id
//        $position_id=M('role')->where(array('role_id'=>$role_id))->getField('position_id');
        $position_id=$role_id;
        //获取用户的的department_id
        $department_id=M('position')->where(array('position_id'=>$position_id))->getField('department_id');

        $d_user = D('UsertasteView');
//        $d_user->select();
//        echo 222;
//        echo M()->getLastSql();
        $where['position.department_id']=$department_id;
        $rs=$d_user->where($where)->getField('position_id',true);

        session('all_roleids',$rs);

        //销售经理的role_id
        $rsId=uniqueSale($rs);

        /**
         * 获取总经理助理的role_id
         */
        $assiant_id=M('role')->where(array('position_id'=>11))->getField('role_id');

        $taste = D("Taste");
        $taste->status = TasteModel::ORDER_TASTE;
        $ret = $taste->where("id=".$id)->save();
        $data = array('code'=>1, 'msg'=>'failed');
        if ($ret) {
            $title="审核通过的订单已制作完成并发送给客户！";
            $value=M()->table("5k_taste t")->join("5k_customer c on c.customer_id=t.customer_id")->where(array('t.id'=>$id,'t.status'=>5))->field("c.name, t.*")->find();
            $content ='<table cellspacing="0" cellpadding="10" border="1px solid #gray" style="background-color: #ebf1f1;">';
            $content .='<thead><th>试吃客户名称</th><th>试吃原因</th><th>试吃人数</th><th>试吃商品</th><th>试吃收获地址</th><th>收货人联系电话</th><th>配送时间</th><th>备注</th></thead>';
            $content .='<tbody >';
            $content.=<<< HTML
<tr><td width="10%" align="center">{$value['name']}</td><td width="20%"  align="center">{$value['reason']}</td><td width="10%" align="center">{$value['taster_num']}</td><td width="10%" align="center">{$value['product']}</td><td width="10%" align="center">{$value['delivery_address']}</td><td width="10%" align="center">{$value['phone_number']}</td><td width="10%" align="center">{$value['delivery_time']}</td><td width="10%" align="center">{$value['memo']}</td></tr>
HTML;
            $content.='</tbody></table>';
            $mailRs=sysSendEmail($role_id,$title,$content,C('defaultinfo.name'));
            try{
                sysSendEmail($role_id,$title,$content,C('defaultinfo.name'));
                sysSendEmail($rsId,$title,$content,C('defaultinfo.name'));
                sysSendEmail($assiant_id,$title,$content,C('defaultinfo.name'));
            }catch (phpmailerException $e){
                echo "邮件发送失败：".$e->errorMessage();
            }
//            if($mailRs){
//                $mailData=array('message','邮件发送成功！');
//            }else{
//                $mailData=array('message','邮件发送失败！');
//            }
//            $data=array('code'=>0,'msg'=>success);
//            $data=array_merge($data,$mailData);
            $data = array('code'=>0, 'msg'=>'success');
        }
        return $this->ajaxReturn($data);
    }

}